<?php
declare(strict_types=1);

namespace App\Common\Controller;

use App\Common\Logic\FileLogic;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use \Hyperf\View\RenderInterface;

class IndexController extends AbstractController
{

    public function index()
    {
        return $this->render->render('/common/index');
    }

    public function uploadByEdit(){
        $uploadLogic = new FileLogic();
        if ($this->request->hasFile('file')) {
            if ($this->request->file('file')->isValid()) {
                $file = $this->request->file('file');
                $getRes = $uploadLogic->uploadFile($file, 'edit');
                if ($getRes['state']){
                    return $this->success(['src'=>$getRes['data']['filePath']],'上传成功');
                }

//                $res = [
//                    'resourceType'=>'Files',
//                    'currentFolder'=>[
//                        'path'=>'/',
//                        'url' =>'/uploads/edit/G8baf9753667d7047863a30166a9d5514/',
//                        'acl' =>255
//                    ],
//                    'fileName'=>'201808270633405540.jpg',
//                    'uploaded'=>1
//                ];
//                return $res;
            }
        }
        return $this->error('上传失败');
    }

    public function jump()
    {
        $data = $this->request->all();
        if (!empty($data['param'])){
            $data = json_decode(base64_decode($data['param']),true);
        }
        if (empty($data)||empty($data['code'])){
            $data = [
                'code'=>400,
                'msg' =>'未知错误',
                'wait'=>'3',
                'url' =>'/'
            ];
        }else{
            $msgwait = ($data['code']==0)?'成功':'未知错误';
            $msg = !empty($data['msg'])?$data['msg']:$msgwait;
            $data = [
                'code'=>$data['code'],
                'msg' =>$msg,
                'wait'=>!empty($data['wait'])?$data['wait']:'3',
                'url' =>!empty($data['url'])?$data['url']:'/'
            ];
        }
        return $this->render->render('/common/dispatch_jump',$data);
    }
}
